import Express from 'express';

import { HTTPError } from '../error';

export const ErrorHandlingMiddleware = (
  error: unknown,
  _req: Express.Request,
  res: Express.Response,
  _next: Express.NextFunction
) => {
  if (error instanceof HTTPError)
    return res.status(error.statusCode).json({ status: error.statusCode, message: error.message });

  console.error(error);
  return res.status(500).json({ status: 500, message: 'Internal Server Error' });
};
